package net.alteiar.images;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

public class BasicImage extends IDrawable {
	protected final int quality;
	private final BufferedImage img;

	public BasicImage(BufferedImage img) {
		this(img, IDrawable.MEDIUM_QUALITY);
	}

	public BasicImage(BufferedImage img, int quality) {
		this.quality = quality;
		this.img = img;
	}

	public BufferedImage getImage() {
		return this.img;
	}

	public int[] getImagePixels() {
		return ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
	}

	@Override
	public int getWidth() {
		return getImage().getWidth();
	}

	@Override
	public int getHeight() {
		return getImage().getHeight();
	}

	@Override
	public void draw(Graphics2D g2, double zoomFactor) {
		switch (quality) {
		case LOW_QUALITY:
			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
			break;
		case MEDIUM_QUALITY:
			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			break;
		case HIGH_QUALITY:
			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BICUBIC);
			break;
		}
		g2.drawImage(img, 0, 0, (int) (getWidth() * zoomFactor),
				(int) (getHeight() * zoomFactor), null);
	}
}
